package Offer018;

/**
 * 有效的回文
 *
 * @author 23737
 * @time 2021.12.9
 */
public class test {
    public static void main(String[] args) {
        System.out.println(new Solution().isPalindrome("A man, a plan, a canal: Panama"));
        System.out.println(new Solution().isPalindrome("0P"));
    }
}

/**
 * 最简答的做法就是直接调用API
 */
class Solution {
    public boolean isPalindrome(String s) {
        String s2 = s.toLowerCase();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < s2.length(); i++) {
            char c = s2.charAt(i);
            if (Character.isLowerCase(c) ||  (c >= '0' && c <= '9')) {
                stringBuilder.append(c);
            }
        }
        String s1 = stringBuilder.reverse().toString();
        return check(s1);
    }

    private boolean check(String s1) {
        StringBuilder stringBuilder = new StringBuilder();
        String s = stringBuilder.append(s1).reverse().toString();
        return s1.equals(s);
    }
}
